package com.igeekhome.controller.api;

import com.github.pagehelper.PageInfo;
import com.igeekhome.pojo.DAdvancedCustomerAddressList;
import com.igeekhome.pojo.DCustomer;
import com.igeekhome.service.ICustomerAddressListService;
import com.igeekhome.service.ICustomerService;
import com.igeekhome.util.ResultBean;
import com.igeekhome.util.TableResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 立以宁
 */
@RestController
@RequestMapping("/api/customer")
public class CustomerControllerApi {

    @Autowired
    private BaseDictControllerApi baseDictControllerApi;

    @Autowired
    private ICustomerService customerService;


    @Autowired
    private ICustomerAddressListService customerAddressListService;



    @RequestMapping("/list")
    public TableResultBean list(@RequestParam(defaultValue = "1")Integer page, @RequestParam(defaultValue = "10")Integer limit, DCustomer dCustomer,@RequestParam(defaultValue = "1")Integer rank) {
        dCustomer.setClientType(rank);
        PageInfo<DCustomer> pageInfo = customerService.selectAll(limit, page, dCustomer);

        return new TableResultBean(0,"成功",pageInfo.getTotal(),pageInfo.getList());
    }

    @RequestMapping("/AddressList/{id}")
    public ResultBean AddressList(@PathVariable Integer id) {

        List<DAdvancedCustomerAddressList> dAdvancedCustomerAddressLists = customerAddressListService.selectAll(id);

        return ResultBean.build(0,"成功",dAdvancedCustomerAddressLists);
    }

    @RequestMapping("/update")
    public ResultBean update(DCustomer dCustomer) {
        customerService.update(dCustomer);
        return  ResultBean.ok();
    }

    @RequestMapping("/add")
    public ResultBean add(DCustomer dCustomer) {
        customerService.add(dCustomer);
        return  ResultBean.ok();
    }

    @RequestMapping("/delete")
    public ResultBean delete(Integer clientId,Integer clientType) {

        customerService.delete(clientId,clientType);

        return  ResultBean.ok();
    }

    /**
     * 高级客户地址的新增和修改
     */


    @RequestMapping("/updateAddress")
    public ResultBean update(DAdvancedCustomerAddressList company) {
        customerAddressListService.updateAddressList(company);
        return  ResultBean.ok();
    }

    @RequestMapping("/addAddress")
    public ResultBean add(DAdvancedCustomerAddressList company) {
        System.out.println("company: "+company);
        customerAddressListService.addAddressList(company);
        return  ResultBean.ok();
    }

}
